<template>
  <div>
    <a-spin :spinning="spinning != 0">
      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button @click="addReader" type="primary" icon="user-add"> {{$t('m.bpm.other.addReadUser')}}</a-button>
        <a-button @click="removeReader" type="default" icon="user-delete"> {{$t('m.bpm.other.subReadUser')}}</a-button>
      </div>

      <!-- table区域-begin -->
      <div>
        <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="pagination"
          @change="handleTableChange"
        >
          <template slot="serialNum" slot-scope="text,record,index">
            {{(pagination.current-1) * pagination.pageSize + parseInt(index) + 1}}
          </template>
        </a-table>
      </div>
    </a-spin>

    <!--流程选人对话框-->
    <bpm-select-user-dialog ref="bpmSelectUserDialog" @selectUserBack="selectUserBack"
                            :getActorsUrl="url.getActors"></bpm-select-user-dialog>
  </div>
</template>

<script>

  import { getAction, putAction } from '@/api/manage'
  import BpmSelectUserDialog from '../bpmButton/BpmSelectUserDialog'
  import { ViewMixin } from '@/mixins/ViewMixin'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'BpmTaskReader',
    mixins: [ViewMixin],
    components: {
      BpmSelectUserDialog,
    },
    props: {
      processInstanceId: {
        type: String,
        default: null
      }
    },
    data () {
      return {
        currentUrl: null,//记录当前动作需要调用的url，回调使用
        url: {
          addReader: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/addReader/v1',
          removeReader: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/removeReader/v1',
          list: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/getTaskReaderListByPage/v1',
          getActors: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/getActors/v1',
          getFirstHistTask: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/getFirstHistTask/v1',
        },
        queryParam: {
          processInstanceId: this.processInstanceId
        },//查询参数
        columns: [
          {
            title:this.$t('m.common.label.number'),
            dataIndex: '',
            key: 'rowIndex',
            width: 50,
            align: 'center',
            scopedSlots: { customRender: 'serialNum' },
          },
          {
            title: this.$t('m.bpm.entity.readerName'),
            align: 'center',
            dataIndex: 'assigneeName',
            width: 180,
            customRender: function (t, r, index) {
              return t + (r.assigneeDeptName ? '/' + r.assigneeDeptName : '')
            }
          },
          {
            title: this.$t('m.bpm.entity.sendTimeString'),
            align: 'center',
            dataIndex: 'sendTimeString',
            width: 130
          },
        ],
      }
    },
    methods: {
      getTask (url) {
        return new Promise((resovle, reject) => {
          this.spinning++
          getAction(url, { processInstanceId: this.processInstanceId }).then((res) => {
            if (res.success) {
              resovle(res.result)
            } else {
              this.messageUtil.warning(res.message)
            }
          }).finally(() => {
            this.spinning--
          })
        })
      },
      /*增加读者*/
      addReader () {
        this.getTask(this.url.getFirstHistTask).then((task) => {
          if (!task) {
            this.messageUtil.warning(this.$t('m.bpm.verify.tip_no_task'))
          } else {
            const button = {
              mustIdea: '2',
              type: 'addReader',
              label: this.$t('m.bpm.other.addReadUser'),
              attribute: {
                taskId: task.id,
              }
            }
            this.currentUrl = this.url.addReader + '?taskId=' + button.attribute.taskId
            this.$refs.bpmSelectUserDialog.open(button)
          }
        })
      },
      /*减少读者*/
      removeReader () {
        this.getTask(this.url.getFirstHistTask).then((task) => {
          if (!task) {
            this.messageUtil.warning(this.$t('m.bpm.verify.tip_no_task'))
          } else {
            const button = {
              mustIdea: '2',
              type: 'removeReader',
              label: this.$t('m.bpm.other.subReadUser'),
              attribute: {
                taskId: task.id,
              }
            }
            this.currentUrl = this.url.removeReader + '?taskId=' + button.attribute.taskId
            this.$refs.bpmSelectUserDialog.open(button)
          }
        })
      },
      /*流程选人框的回调，执行按钮实际动作*/
      selectUserBack (data) {
        this.spinning++
        putAction(this.currentUrl, data).then((res) => {
          if (res.success) {
            this.messageUtil.success(res.message)
            this.loadData()
          } else {
            this.messageUtil.warning(res.message)
          }
        }).finally(() => {
          this.spinning--
        })
      },
    }
  }
</script>
